package com.itbaizhan.shopping_cartgoods_service.service.impl;

import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.service.CartGoodsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@DubboService
@Transactional
public class CartGoodsServiceImpl implements CartGoodsService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<CartGoods> findAll(Long id) {
        List<CartGoods> cartGoodsList = (List) redisTemplate.opsForHash().get("cartList", id);
        if(cartGoodsList == null) {
            return new ArrayList<>();
        }
        return cartGoodsList;
    }

    @Override
    public void addCartGoods(Long id, CartGoods cartGoods) {
        // 查询出该用户的购物车
        List<CartGoods> cartGoodsList = findAll(id);
        //如果该用户购物车中有该商品，则修改商品数量
        for (CartGoods goods : cartGoodsList) {
            if(goods.getGoodId() == cartGoods.getGoodId()) {
                goods.setNum(cartGoods.getNum()+goods.getNum());
                redisTemplate.boundHashOps("cartList").put(id,cartGoodsList);
                return;
            }
        }
        // 如果该用户购物车中没有该商品，则添加到购物车中
        cartGoodsList.add(cartGoods);
        redisTemplate.boundHashOps("cartList").put(id,cartGoodsList);

    }

    @Override
    public void updateCartGoodsNum(Long userId, Long goodId, Integer num) {
        List<CartGoods> cartGoodsList = findAll(userId);
        for(CartGoods cartGoods : cartGoodsList){
            if(Objects.equals(goodId, cartGoods.getGoodId())) {
                cartGoods.setNum(num);
                break;
            }
        }
        redisTemplate.opsForHash().put("cartList",userId,cartGoodsList);
    }

    @Override
    public void deleteCartGoods(Long userId, Long goodId) {
        List<CartGoods> cartGoodsList = findAll(userId);
        for(CartGoods cartGoods : cartGoodsList){
            if(Objects.equals(goodId, cartGoods.getGoodId())) {
                cartGoodsList.remove(cartGoods);
                break;
            }
        }
        redisTemplate.opsForHash().put("cartList",userId,cartGoodsList);
    }

    @Override
    public void refreshCartGoods(CartGoods cartGoods) {
        Long goodId = cartGoods.getGoodId();
        // 获取到所有用户购物车
        BoundHashOperations boundHashOps = redisTemplate.boundHashOps("cartList");
        Map<Long,List<CartGoods>> allCartGoods = boundHashOps.entries();
        Collection<List<CartGoods>> values = allCartGoods.values();

        //遍历所有用户的购物车
        for (List<CartGoods> goodsList : values) {
            for (CartGoods goods : goodsList) {
                if(goods.getGoodId().equals(goodId)) {
                    goods.setGoodsName(cartGoods.getGoodsName());
                    goods.setHeaderPic(cartGoods.getHeaderPic());
                    goods.setPrice(cartGoods.getPrice());
                    break;
                }
            }
        }
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }

    @Override
    public void deleteCartGoods(Long goodId) {
        // 获取到所有用户购物车
        BoundHashOperations boundHashOps = redisTemplate.boundHashOps("cartList");
        Map<Long,List<CartGoods>> allCartGoods = boundHashOps.entries();
        Collection<List<CartGoods>> values = allCartGoods.values();

        //遍历所有用户的购物车
        for (List<CartGoods> goodsList : values) {
            for (CartGoods goods : goodsList) {
                if(goods.getGoodId().equals(goodId)) {
                    goodsList.remove(goods);
                    break;
                }
            }
        }
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }
}
